package com.quo.controller;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.quo.dto.QuoDto;
import com.quo.dto.QuoInfo;
import com.quo.dto.QuotesDto;
import com.quo.entity.Emp;
import com.quo.entity.Order;
import com.quo.entity.Product;
import com.quo.entity.QuoDetail;
import com.quo.entity.Quote;
import com.quo.service.OrderService;
import com.quo.service.QuoteService;
import com.quo.service.SystemService;
import com.quo.utils.Result;
import com.quo.utils.ResultCode;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/** 

* @author zhoumin

*/
@RestController("orderController")
@RequestMapping(value="/api")
public class OrderController {
	
	
	@Autowired
	public OrderService oservice;
	
	@Autowired
	public SystemService systemService;
	
	
	//获取所有订单信息
	  @GetMapping("/order-all")
	  public Result getOrderList(HttpServletRequest request){ 
		  Cookie[] cookies = request.getCookies();	
		  Integer eno = 0;
		  String pwd = null;
		  for(Cookie cookie : cookies) {
			  String name = cookie.getName();
			  String value = cookie.getValue();         	  		    
		    if(name.equals("a")) {
		    	String id = cookie.getValue(); 
		    	eno = Integer.valueOf(id);		       	
		    }		    
		    else if(name.equals("b")) {
		    	pwd = cookie.getValue();		    		    	
		    }			  	          
		  }	  		
			  Result result = new Result();
			  Emp emp = systemService.ifExists(eno,pwd);	
			  List<Order> olist =  oservice.getOrderList(); 
			  System.out.println("jieguo1"+olist);
			  result = new Result(ResultCode.SUCCESS);
			  result.setData(olist);   
			  return result;		  
			  
	
	  
	  }
	  
//	//获取所有报价单信息（个人）
//	  @GetMapping("/quote-personal")
//	  public Result getQuoteListForSelf(HttpServletRequest request){ 
//		  Cookie[] cookies = request.getCookies();	
//		  Integer eno = 0;
//		  String pwd = null;
//		  for(Cookie cookie : cookies) {
//			  String name = cookie.getName();
//			  String value = cookie.getValue();         	  		    
//		    if(name.equals("a")) {
//		    	String id = cookie.getValue(); 
//		    	eno = Integer.valueOf(id);		       	
//		    }		    
//		    else if(name.equals("b")) {
//		    	pwd = cookie.getValue();		    		    	
//		    }			  	          
//		  }	  		
//		  Result result = new Result();
//		  Emp emp = systemService.ifExists(eno,pwd);	
//			  List<QuotesDto> qlistForSelf = qService.getQuoteListForSelf(eno);
//			  result = new Result(ResultCode.SUCCESS);
//			  result.setData(qlistForSelf);
//			  return result;
//	  }
//	
//	  		//获取单个详细报价单信息
//		  @GetMapping("/quote-all/{qid}")
//		  public Result getQuote(@PathVariable Long qid){ 
//			  
//			  QuoDto quoDto =	qService.getQuoDto(qid); 
//			  List<QuoDetail> quoDetail = qService.getQuoDetailList(qid);
//			  quoDto.setQuoDetail(quoDetail);
//			  Result result=new Result(ResultCode.SUCCESS);
//			  result.setData(quoDto);     
//			  System.out.println(result);
//			  return result;
//		  
//		  }
//		  
//			//获取单个详细报价单信息（个人）
//		  @GetMapping("/quote-personal/{qid}")
//		  public Result getQuoteForSelf(@PathVariable Long qid,HttpServletRequest request){ 
//			  Cookie[] cookies = request.getCookies();	
//			  Integer eno = 0;
//			  String pwd = null;
//			  for(Cookie cookie : cookies) {
//				  String name = cookie.getName();
//				  String value = cookie.getValue();         	  		    
//			    if(name.equals("a")) {
//			    	String id = cookie.getValue(); 
//			    	eno = Integer.valueOf(id);		       	
//			    }		    
//			    else if(name.equals("b")) {
//			    	pwd = cookie.getValue();		    		    	
//			    }			  	          
//			  }	  		
//			  Emp emp = systemService.ifExists(eno,pwd);
//			  QuoDto quoDto =	qService.getQuoDtoForSelf(qid,eno); 
//			  List<QuoDetail> quoDetail = qService.getQuoDetailList(qid);
//			  quoDto.setQuoDetail(quoDetail);
//			  Result result=new Result(ResultCode.SUCCESS);
//			  result.setData(quoDto);     
//			  System.out.println(result);
//			  return result;
//		  
//		  }
//		  
//		  //添加新报价单
//		  @PostMapping("/quote")
//		  public Result addQuoDto(HttpSession session,@RequestBody QuoInfo quoInfo){ 
//			  Emp emp = (Emp)session.getAttribute("emp");
//			  
//			  quoInfo.getQuote().setCeno(emp.getEno());
//			  qService.addQuoInfo(quoInfo.getQuote(),quoInfo.getQuoteDetail());      
//			  
//			  return new Result(ResultCode.SUCCESS);
//		  
//		  }
//		  
//		  
//		//删除报价单
//		  @DeleteMapping("/quote/{qid}")
//		  public Result delQuoteInfo(@PathVariable(value="qid") Long qid){ 
//			  qService.delQuoteInfo(qid); 
//			  
//			  return new Result(ResultCode.SUCCESS);
//		  
//		  }
//	
//	
	
//	  /**
//	     * 打印员工pdf报表x
//	     */
//	    @RequestMapping(value="/{id}/pdf",method = RequestMethod.GET)
//	    public void pdf(@PathVariable String id) throws IOException {
//	        //1.引入jasper文件
//	        Resource resource = new ClassPathResource("templates/profile.jasper");
//	        FileInputStream fis = new FileInputStream(resource.getFile());
//
//	        //2.构造数据
//	        //a.用户详情数据
//	        UserCompanyPersonal personal = userCompanyPersonalService.findById(id);
//	        //b.用户岗位信息数据
//	        UserCompanyJobs jobs = userCompanyJobsService.findById(id);
//	        //c.用户头像        域名 / id
//	        String staffPhoto = "http://pkbivgfrm.bkt.clouddn.com/"+id;
//
//	        System.out.println(staffPhoto);
//
//	        //3.填充pdf模板数据，并输出pdf
//	        Map params = new HashMap();
//
//
//
//	        Map<String, Object> map1 = BeanMapUtils.beanToMap(personal);
//	        Map<String, Object> map2 = BeanMapUtils.beanToMap(jobs);
//
//	        params.putAll(map1);
//	        params.putAll(map2);
//	        params.put("staffPhoto","staffPhoto");
//
//	        ServletOutputStream os = response.getOutputStream();
//	        try {
//	            JasperPrint print = JasperFillManager.fillReport(fis, params,new JREmptyDataSource());
//	            JasperExportManager.exportReportToPdfStream(print,os);
//	        } catch (JRException e) {
//	            e.printStackTrace();
//	        }finally {
//	            os.flush();
//	        }
//	    }
	  
//	  /**
//	     *  基于JDBC数据源的形式填充数据
//	     */
//	    @GetMapping("/testJasper3")
//	    public void createPdf(HttpServletRequest request, HttpServletResponse response) throws Exception {
//	        //1.引入jasper文件
//	        Resource resource = new ClassPathResource("templates/testConn.jasper");
//	        FileInputStream fis = new FileInputStream(resource.getFile());
//
//	        //2.创建JasperPrint,向jasper文件中填充数据
//	        ServletOutputStream os = response.getOutputStream();
//	        try {
//	            Map parameters = new HashMap<>();
//	            //查询企业id为1的所有用户
//	            parameters.put("cid","1");
//
//	            //获取数据库连接
//	            Connection conn= getConnection();
//	            JasperPrint print = JasperFillManager.fillReport(fis,parameters,conn);
//	            JasperExportManager.exportReportToPdfStream(print,os);
//	        } catch (JRException e) {
//	            e.printStackTrace();
//	        }finally {
//	            os.flush();
//	        }
//	    }
//
//	    public Connection getConnection() throws Exception {
//	        Class.forName("oracle.jdbc.driver.OracleDriver");
//	        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@82.157.249.88:1521/xe","tofu15","LxUaqVk5bNobMGLyDwnY");
//	        return conn;
//	    }

}
